package com.anbinh.doctor.adapter;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.fragment.benhnhan.FragmentListPatients;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ImageData;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.PatientOptionType;
import com.anbinh.doctor.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class PatientImagesAdapter extends ArrayAdapter<ImageData> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<ImageData> listData;
	private ImageLoader imageLoader;
	private Context mContext;
	private UserLogin userLogin;

	public PatientImagesAdapter(Context mContext, int resource,
			List<ImageData> models) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;
		layoutInflater = LayoutInflater.from(mContext);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(mContext
				.getApplicationContext()));
		userLogin = Utils.getAccountModel(mContext, Utils.KEY_USER);

	}

	public class ViewHolder {
		public TextView lblName;
		public TextView lblTime;
		public ImageView icon;
		public View btnRemove;

	}

	@Override
	public ImageData getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.lblName = (TextView) convertView
					.findViewById(R.id.lblName);
			viewHolder.lblTime = (TextView) convertView
					.findViewById(R.id.lblTime);
			viewHolder.btnRemove = (View) convertView
					.findViewById(R.id.btnRemove);

			viewHolder.icon = (ImageView) convertView
					.findViewById(R.id.drag_handle);

			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final ImageData item = getItem(position);
		Log.d("lblTitle", item.getId() + "");
		viewHolder.lblName.setText(item.getCaption());
		viewHolder.lblTime.setText(item.getCreated_at());
		Bitmap bmp = imageLoader.loadImageSync(item.getThumb());
		if (bmp == null) {
			imageLoader.displayImage(item.getThumb(), viewHolder.icon,
					Utils.getOptions(mContext),
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
						}
					});
		} else {
			viewHolder.icon.setImageBitmap(bmp);
		}

		viewHolder.btnRemove.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPopupMenu(item, position);
			}
		});
		return convertView;
	}

	public void showPopupMenu(final ImageData item, final int location) {
		final Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(
				R.drawable.abs__menu_dropdown_panel_holo_light);
		dialog.setCanceledOnTouchOutside(true);
		DisplayMetrics metrics = new DisplayMetrics();
		((Activity) mContext).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		int width = metrics.widthPixels;

		dialog.setContentView(R.layout.dialog_menu_extaned);
		dialog.getWindow().setLayout(width - 60, LayoutParams.WRAP_CONTENT);

		TextView lblXoaDc = (TextView) dialog.findViewById(R.id.lblXoaDc);
		lblXoaDc.setText("Xóa ảnh");

		lblXoaDc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onDelete(item);
				dialog.dismiss();
			}
		});

		dialog.show();
	}

	protected void onDelete(final ImageData item) {

		ServerController.getInstance(mContext).reuquestRemoveImagePatient(
				userLogin.getData().getToken(),
				FragmentListPatients.newInstance().getCurrentPatient().getId(),
				item.getId(),
				FragmentListPatients.newInstance().getCurrentDoctorId(),
				PatientOptionType.IMAGE.getCode(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						listData.remove(item);
						notifyDataSetChanged();
					}

					@Override
					public void onError(int errorCode, String message) {

					}

				});
	}

	public void onChangeActive(ImageData item) {

		ServerController.getInstance(mContext).requestChangeActive(
				userLogin.getData().getToken(), item.getId(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
					}

					@Override
					public void onError(int errorCode, String message) {

					}

				});

	}

}
